﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TicketingSystem
{
    public class TicketingEnchancements
    {
        private static void SaveContext(TicketingDatabaseEntities context)
        {
            try
            {
                context.SaveChanges();
            }
            catch (Exception e)
            {
                Trace.WriteLine(DateTime.Now + "\t" + e.InnerException);
                //Debug.WriteLine(DateTime.Now + "\t" + e.InnerException);
                //Console.WriteLine(DateTime.Now + "\t" + e.InnerException);
            }
        }

        #region CategoryDAL

        public static void AddCatiegory(Category category)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                context.Categories.AddObject(category);

                SaveContext(context);
            }
        }

        public static void DeleteCategory(int categoryID)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                Category categoryToDelete = context.Categories.FirstOrDefault(c => c.CategoryID == categoryID);

                if (categoryToDelete != null)
                {
                    context.Attach(categoryToDelete);
                    context.Categories.DeleteObject(categoryToDelete);
                }

                SaveContext(context);
            }
        }

        public static void UpdateCategory(int categoryID, Category newCategory)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                Category categoryToUpdate = context.Categories.FirstOrDefault(c => c.CategoryID == categoryID);

                if (categoryToUpdate != null)
                {
                    context.Attach(categoryToUpdate);
                    categoryToUpdate.CategoryName = newCategory.CategoryName;
                }

                SaveContext(context);
            }
        }

        public static Category GetCategoryByID(int categoryID)
        {
            Category category = null;

            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                category = context.Categories.FirstOrDefault(c => c.CategoryID == categoryID);
            }

            return category;
        }

        public static IEnumerable<Category> GetCategories()
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                var categories = context.Categories.OrderBy(c => c.CategoryName).ToList();

                return categories;
            }
        }

        #endregion

        #region LocationDAL

        public static void AddLocation(Location location)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                context.Locations.AddObject(location);

                SaveContext(context);
            }
        }

        public static void DeleteLocation(int locationID)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                Location locationToDelete = context.Locations.FirstOrDefault(l => l.LocationID == locationID);

                if (locationToDelete != null)
                {
                    context.Attach(locationToDelete);
                    context.Locations.DeleteObject(locationToDelete);
                }

                SaveContext(context);
            }
        }

        public static void UpdateLocation(int locationID, Location newLocation)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                Location locationToUpdate = context.Locations.FirstOrDefault(l => l.LocationID == locationID);

                if (locationToUpdate != null)
                {
                    context.Attach(locationToUpdate);
                    locationToUpdate.LocationName = newLocation.LocationName;
                }

                SaveContext(context);
            }
        }

        public static Location GetLocationByID(int locationID)
        {
            Location location = null;

            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                location = context.Locations.FirstOrDefault(l => l.LocationID == locationID);
            }

            return location;
        }

        public static IEnumerable<Location> GetLocations()
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                var locations = context.Locations.ToList();

                return locations;
            }
        }

        #endregion

        #region EventDAL

        public static void AddEvent(Event eventToAdd)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                context.Events.AddObject(eventToAdd);

                SaveContext(context);
            }
        }

        public static void DeleteEvent(int eventID)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                Event eventToDelete = context.Events.FirstOrDefault(e => e.EventID == eventID);

                if (eventToDelete != null)
                {
                    context.Attach(eventToDelete);
                    context.Events.DeleteObject(eventToDelete);
                }

                SaveContext(context);
            }
        }

        public static void UpdateEvent(int eventID, Event newEvent)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                Event eventToUpdate = context.Events.FirstOrDefault(e => e.EventID == eventID);

                if (eventToUpdate != null)
                {
                    eventToUpdate.CategoryID = newEvent.CategoryID;
                    eventToUpdate.EventTime = newEvent.EventTime;
                    eventToUpdate.EventTitle = newEvent.EventTitle;
                    eventToUpdate.LocationID = newEvent.LocationID;
                    eventToUpdate.Price = newEvent.Price;
                }

                SaveContext(context);
            }
        }

        public static Event GetEventByID(int eventID)
        {
            Event eventToReturn = null;

            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                eventToReturn = context.Events.FirstOrDefault(e => e.EventID == eventID);
            }

            return eventToReturn;
        }

        public static IEnumerable<Event> GetAllEvents()
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                //TicketingDatabaseEntities context = new TicketingDatabaseEntities();
                DateTime date = DateTime.Now.AddDays(-5000);

                var events = context.Events.Include("Category").Include("Location").OrderBy(e => e.EventTime);

                return events.ToList();
            }
        }

        public static IEnumerable<Event> GetEventsByCategoryID(int categoryID)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                DateTime date = DateTime.Now.AddDays(-5000);

                var events = from e in context.Events.Include("Category").Include("Location")
                             where e.EventTime > date && e.CategoryID == categoryID
                             orderby e.EventTime
                             select e;

                return events.ToList();
            }
        }

        #endregion

        #region PurchaseDAL

        public static void CreatePurchase(Purchase purchase)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                context.Purchases.AddObject(purchase);

                SaveContext(context);
            }
        }

        public static IEnumerable<Purchase> GetPurchasesByStatus(bool delivered)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                var purchases = from p in context.Purchases
                                where p.Delivered == delivered
                                select p;

                return purchases.ToList();
            }
        }

        public static Purchase GetPurchaseByID(int purchaseID)
        {
            Purchase purchase = null;

            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                purchase = context.Purchases.FirstOrDefault(p => p.PurchaseID == purchaseID);

                return purchase;
            }
        }

        public static void UpdatePurchase(int purchaseID, bool delivered)
        {
            using (TicketingDatabaseEntities context = new TicketingDatabaseEntities())
            {
                Purchase purchase = context.Purchases.FirstOrDefault(p => p.PurchaseID == purchaseID);

                if (purchase != null)
                {
                    context.Attach(purchase);
                    purchase.Delivered = delivered;
                }

                SaveContext(context);
            }
        }

        #endregion
    }
}
